﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using bzxxxt.admin.entity;
using bzxxxt.admin.service;
using bzxxxt.dal;
using bzxxxt.model;

namespace bzxxxt.bll
{
    public class HzBLL:BaseBLL
    {
        UserEntity user = UserService.GetUser(HttpContext.Current);

        public tbParadise getParadise(int pid)
        {
            return DB.tbParadise.FirstOrDefault(m => m.id == pid);
        }
        public tbParadise getParadise(string code)
        {
            return DB.tbParadise.FirstOrDefault(m => m.code == code);
        }
        public string getAreaNameByCode(string code) {
            var area = DB.tbArea.FirstOrDefault(m => m.code == code);
            if (area == null) return "全市";
            else return "【"+area.name.Trim()+"】";
        }
        
        public string hzBaseAreaByUserParadise(string paradisecode)
        {
            var list = GetHzBaseAreaByUserParadise(paradisecode);
            return JsonUtil.ToJson(list);
        }
        public List<HzBaseModel> GetHzBaseAreaByUserParadise(string paradisecode)
        {
            var p = DB.tbParadise.FirstOrDefault(m => m.code == paradisecode);

            var list = DB.hzBaseAreaByUserParadise(user.id, p.id).Select(m => new HzBaseModel()
            {
                pcode = m.gname + "",
                pname = m.gname,
                totaldeads = m.totaldeads,
                totalgraves = m.totalgraves,
                storedeads = m.storedeads,
                singlegraves = m.singlegraves,
                doublegraves = m.doublegraves,
                multigraves = m.multigraves,
                occupygraves = m.occupygraves,
                emptydeads = m.emptydeads,
                emptygraves = m.emptygraves
            }).ToList();
            HzBaseModel allModel = new HzBaseModel
            {
                pcode = "",
                pname = "合计",
                totaldeads = list.Sum(m => m.totaldeads),
                totalgraves = list.Sum(m => m.totalgraves),
                storedeads = list.Sum(m => m.storedeads),
                singlegraves = list.Sum(m => m.singlegraves),
                doublegraves = list.Sum(m => m.doublegraves),
                multigraves = list.Sum(m => m.multigraves),
                occupygraves = list.Sum(m => m.occupygraves),
                emptygraves = list.Sum(m => m.emptygraves),
                emptydeads = list.Sum(m => m.emptydeads)

            };
            list.Add(allModel);
            return list;
        }

        public string hzBaseAreaByUserTypeZheng(string type,string zheng )
        {
            var list = GetHzBaseAreaByUserTypeZheng(type, zheng);
            return JsonUtil.ToJson(list);
        }
        public List<HzBaseModel> GetHzBaseAreaByUserTypeZheng(string type, string zheng)
        {
            if (string.IsNullOrEmpty(type)) type = null;
            if (string.IsNullOrEmpty(zheng)) zheng = null;
            var list = DB.hzBaseAreaByUserTypeZheng(user.id, type, zheng).Select(m => new HzBaseModel()
            {
                pcode = m.gname + "",
                pname = m.gname,
                totaldeads = m.totaldeads,
                totalgraves = m.totalgraves,
                storedeads = m.storedeads,
                singlegraves = m.singlegraves,
                doublegraves = m.doublegraves,
                multigraves = m.multigraves,
                occupygraves = m.occupygraves,
                emptydeads = m.emptydeads,
                emptygraves = m.emptygraves
            }).ToList();
            HzBaseModel allModel = new HzBaseModel
            {
                pcode = "",
                pname = "合计",
                totaldeads = list.Sum(m => m.totaldeads),
                totalgraves = list.Sum(m => m.totalgraves),
                storedeads = list.Sum(m => m.storedeads),
                singlegraves = list.Sum(m => m.singlegraves),
                doublegraves = list.Sum(m => m.doublegraves),
                multigraves = list.Sum(m => m.multigraves),
                occupygraves = list.Sum(m => m.occupygraves),
                emptygraves = list.Sum(m => m.emptygraves),
                emptydeads = list.Sum(m => m.emptydeads)

            };
            list.Add(allModel);
            return list;
        }
        
        public String hzBaseParadiseByUserTypeZheng(string type, string areacode)
        {
            var list = GetHzBaseParadiseByUserTypeZhengData(type, areacode);
            return JsonUtil.ToJson(list);
        }
        public List<HzBaseModel> GetHzBaseParadiseByUserTypeZhengData(string type, string areacode)
        {
            if (string.IsNullOrEmpty(type) || type=="all") type = null;
            if (string.IsNullOrEmpty(areacode)) areacode = null;
            var list = DB.hzBaseParadiseByUserTypeZheng(user.id, type, areacode).Select(m => new HzBaseModel()
            {
                pcode = m.pid + "",
                pname = m.pname,
                totaldeads = m.totaldeads,
                totalgraves = m.totalgraves,
                storedeads = m.storedeads,
                singlegraves = m.singlegraves,
                doublegraves = m.doublegraves,
                multigraves = m.multigraves,
                occupygraves = m.occupygraves,
                emptydeads = m.emptydeads,
                emptygraves = m.emptygraves
            }).ToList();
            HzBaseModel allModel = new HzBaseModel
            {
                pcode = "",
                pname = "合计",
                totaldeads = list.Sum(m => m.totaldeads),
                totalgraves = list.Sum(m => m.totalgraves),
                storedeads = list.Sum(m => m.storedeads),
                singlegraves = list.Sum(m => m.singlegraves),
                doublegraves = list.Sum(m => m.doublegraves),
                multigraves = list.Sum(m => m.multigraves),
                occupygraves = list.Sum(m => m.occupygraves),
                emptygraves = list.Sum(m => m.emptygraves),
                emptydeads = list.Sum(m => m.emptydeads)

            };
            list.Add(allModel);
            return list;
        }

        public string hzBaseSectionByUserParadise(int pid)
        {
            var p = DB.tbParadise.FirstOrDefault(m => m.id == pid);
            var list = GetHzBaseSectionByUserParadise(p.code);
            return JsonUtil.ToJson(list);
        }
        public string hzBaseSectionByUserParadise(string paradisecode)
        {
            var list = GetHzBaseSectionByUserParadise(paradisecode);
            return JsonUtil.ToJson(list);
        }
        public List<HzBaseModel> GetHzBaseSectionByUserParadise(string paradisecode)
        {
            var p = DB.tbParadise.FirstOrDefault(m => m.code == paradisecode);
            var list = DB.hzBaseSectionByUserParadise(user.id, p.id).Select(m => new HzBaseModel()
            {
                pcode = m.gname + "",
                pname = m.gname,
                totaldeads = m.totaldeads,
                totalgraves = m.totalgraves,
                storedeads = m.storedeads,
                singlegraves = m.singlegraves,
                doublegraves = m.doublegraves,
                multigraves = m.multigraves,
                occupygraves = m.occupygraves,
                emptydeads = m.emptydeads,
                emptygraves = m.emptygraves
            }).ToList();
            list.ForEach(m => {
                string code = m.pcode.Substring(0, 3);
                string deng = m.pcode.Substring(3, 1);
                string floor = m.pcode.Substring(4, 1);
                string sec = m.pcode.Substring(5, 2);

                if (p.type != "骨灰堂")
                {

                    m.pname = string.Format("{0}{1}区", floor, sec);
                }
                else
                {
                    m.pname = string.Format("{0}栋{1}楼{2}区", deng, floor, sec);
                }
            });
            HzBaseModel allModel = new HzBaseModel
            {
                pcode = "",
                pname = "合计",
                totaldeads = list.Sum(m => m.totaldeads),
                totalgraves = list.Sum(m => m.totalgraves),
                storedeads = list.Sum(m => m.storedeads),
                singlegraves = list.Sum(m => m.singlegraves),
                doublegraves = list.Sum(m => m.doublegraves),
                multigraves = list.Sum(m => m.multigraves),
                occupygraves = list.Sum(m => m.occupygraves),
                emptygraves = list.Sum(m => m.emptygraves),
                emptydeads = list.Sum(m => m.emptydeads)

            };
            list.Add(allModel);
            return list;
        }        
        

        
        public string hzMonthAreaByUserParadiseYear(string paradisecode, int? year)
        {
            var list = GetHzMonthAreaByUserParadiseYear(paradisecode, year);
            return JsonUtil.ToJson(list);
        }
        public List<HzByYearModel> GetHzMonthAreaByUserParadiseYear(string paradisecode, int? year)
        {
            var p = DB.tbParadise.FirstOrDefault(m => m.code == paradisecode);
            var list = DB.hzMonthAreaByUserParadiseYear(user.id, p.id, year).Select(m => new HzByYearModel
            {
                pcode = m.code,
                pname = m.name,
                m1 = m.m1,
                m2 = m.m2,
                m3 = m.m3,
                m4 = m.m4,
                m5 = m.m5,
                m6 = m.m6,
                m7 = m.m7,
                m8 = m.m8,
                m9 = m.m9,
                m10 = m.m10,
                m11 = m.m11,
                m12 = m.m12,
                mm = m.mm

            }).ToList();


            var hz = new HzByYearModel
            {
                pname = "合计",
                m1 = list.Sum(m => m.m1),
                m2 = list.Sum(m => m.m2),
                m3 = list.Sum(m => m.m3),
                m4 = list.Sum(m => m.m4),
                m5 = list.Sum(m => m.m5),
                m6 = list.Sum(m => m.m6),
                m7 = list.Sum(m => m.m7),
                m8 = list.Sum(m => m.m8),
                m9 = list.Sum(m => m.m9),
                m10 = list.Sum(m => m.m10),
                m11 = list.Sum(m => m.m11),
                m12 = list.Sum(m => m.m12),
                mm = list.Sum(m => m.mm)
            };

            for (int i = 0; i < list.Count; i++)
            {
                var o = list[i];
                if (o.pcode.Substring(9, 3) != "000") continue;
                string code = o.pcode.Substring(0, 9);
                o.m1 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m1);
                o.m2 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m2);
                o.m3 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m3);
                o.m4 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m4);
                o.m5 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m5);
                o.m6 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m6);
                o.m7 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m7);
                o.m8 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m8);
                o.m9 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m9);
                o.m10 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m10);
                o.m11 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m11);
                o.m12 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m12);
                o.mm = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.mm);
            }
            list.Add(hz);
            return list;
        }

        public string hzMonthAreaByUserTypeZhengYear(string type, string areacode, int? year)
        {
            var list = GetHzMonthAreaByUserTypeZhengYearData(type,areacode,year);
            return JsonUtil.ToJson(list);
        }
        public List<HzByYearModel> GetHzMonthAreaByUserTypeZhengYearData(string type,string areacode, int? year)
        {
            if (string.IsNullOrEmpty(type) || type == "all") type = null;
            if (string.IsNullOrEmpty(areacode)) areacode = null;
            var list = DB.hzMonthAreaByUserTypeZhengYear(user.id,type, areacode, year).Select(m => new HzByYearModel
            {                
                pcode = m.code,
                pname = m.name,
                m1 = m.m1,
                m2 = m.m2,
                m3 = m.m3,
                m4 = m.m4,
                m5 = m.m5,
                m6 = m.m6,
                m7 = m.m7,
                m8 = m.m8,
                m9 = m.m9,
                m10 = m.m10,
                m11 = m.m11,
                m12 = m.m12,
                mm = m.mm

            }).ToList();
            

            var hz = new HzByYearModel
            {
                pname = "合计",
                m1 = list.Sum(m => m.m1),
                m2 = list.Sum(m => m.m2),
                m3 = list.Sum(m => m.m3),
                m4 = list.Sum(m => m.m4),
                m5 = list.Sum(m => m.m5),
                m6 = list.Sum(m => m.m6),
                m7 = list.Sum(m => m.m7),
                m8 = list.Sum(m => m.m8),
                m9 = list.Sum(m => m.m9),
                m10 = list.Sum(m => m.m10),
                m11 = list.Sum(m => m.m11),
                m12 = list.Sum(m => m.m12),
                mm = list.Sum(m => m.mm)
            };

            for (int i = 0; i < list.Count; i++) {
                var o = list[i];                
                if (o.pcode.Substring(9, 3) != "000")  continue;
                string code = o.pcode.Substring(0, 9);
                o.m1 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m1);
                o.m2 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m2);
                o.m3 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m3);
                o.m4 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m4);
                o.m5 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m5);
                o.m6 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m6);
                o.m7 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m7);
                o.m8 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m8);
                o.m9 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m9);
                o.m10 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m10);
                o.m11 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m11);
                o.m12 = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.m12);
                o.mm = list.Where(m => m.pcode.StartsWith(code)).Sum(m => m.mm);
            }
            list.Add(hz);
            return list;
        }

        public string hzMonthParadiseByUserTypeZhengYear(int? year, string type = null, string zheng = null)
        {

            var list = GetHzMonthParadiseByUserTypeZhengYearData(year, type, zheng);
            return JsonUtil.ToJson(list);
        }
        public List<HzByYearModel> GetHzMonthParadiseByUserTypeZhengYearData(int? year, string type = null, string zheng = null)
        {
            if (type == "all" || string.IsNullOrEmpty(type)) type = null;
            if (string.IsNullOrEmpty(zheng)) zheng = null;
            var list = DB.hzMonthParadiseByUserTypeZhengYear(user.id, type, zheng, year).Select(m => new HzByYearModel
            {
                pname = m.name,
                pcode = m.code,
                m1 = m.m1,
                m2 = m.m2,
                m3 = m.m3,
                m4 = m.m4,
                m5 = m.m5,
                m6 = m.m6,
                m7 = m.m7,
                m8 = m.m8,
                m9 = m.m9,
                m10 = m.m10,
                m11 = m.m11,
                m12 = m.m12,
                mm = m.mm

            }).ToList();

            var hz = new HzByYearModel
            {
                pname = "合计",
                m1 = list.Sum(m => m.m1),
                m2 = list.Sum(m => m.m2),
                m3 = list.Sum(m => m.m3),
                m4 = list.Sum(m => m.m4),
                m5 = list.Sum(m => m.m5),
                m6 = list.Sum(m => m.m6),
                m7 = list.Sum(m => m.m7),
                m8 = list.Sum(m => m.m8),
                m9 = list.Sum(m => m.m9),
                m10 = list.Sum(m => m.m10),
                m11 = list.Sum(m => m.m11),
                m12 = list.Sum(m => m.m12),
                mm = list.Sum(m => m.mm)
            };
            list.Add(hz);
            return list;
        }


        public string hzMonthSectionByUserParadiseYear(string paradisecode, int? year)
        {
            var list = GetHzMonthSectionByUserParadiseYearData(paradisecode,year);
            return JsonUtil.ToJson(list);
        }
        public List<HzByYearModel> GetHzMonthSectionByUserParadiseYearData(string paradisecode,int? year)
        {
            var p = DB.tbParadise.FirstOrDefault(m => m.code == paradisecode);
            
            var list = DB.hzMonthSectionByUserParadiseYear(user.id, p.id, year).Select(m => new HzByYearModel
            {
                pname = m.name,
                pcode = m.code,
                m1 = m.m1,
                m2 = m.m2,
                m3 = m.m3,
                m4 = m.m4,
                m5 = m.m5,
                m6 = m.m6,
                m7 = m.m7,
                m8 = m.m8,
                m9 = m.m9,
                m10 = m.m10,
                m11 = m.m11,
                m12 = m.m12,
                mm = m.mm

            }).ToList();

            list.ForEach(m =>
            {
                string code = m.pcode.Substring(0, 3);                
                string deng = m.pcode.Substring(3, 1);
                string floor = m.pcode.Substring(4, 1);
                string sec = m.pcode.Substring(5, 2);
                
                if (p.type != "骨灰堂")
                {
                    m.pname = string.Format("{0}{1}区", floor,sec);
                }
                else
                {
                    m.pname = string.Format("{0}栋{1}楼{2}区", deng, floor, sec);
                }
            });

            var hz = new HzByYearModel
            {
                pname = "合计",
                m1 = list.Sum(m => m.m1),
                m2 = list.Sum(m => m.m2),
                m3 = list.Sum(m => m.m3),
                m4 = list.Sum(m => m.m4),
                m5 = list.Sum(m => m.m5),
                m6 = list.Sum(m => m.m6),
                m7 = list.Sum(m => m.m7),
                m8 = list.Sum(m => m.m8),
                m9 = list.Sum(m => m.m9),
                m10 = list.Sum(m => m.m10),
                m11 = list.Sum(m => m.m11),
                m12 = list.Sum(m => m.m12),
                mm = list.Sum(m => m.mm)
            };
            list.Add(hz);
            return list;
        }

    }
}